/*************************************************************************
	> File Name: 8-2.c
	> Author: Angel Criss
	> Mail: abbytsing@gmail.com 
	> Created Time: Sun Jul 27 13:21:43 2014
 ************************************************************************/

#include <pwd.h>
#include <string.h>
#include <stdio.h>
#include <stdio.h>
#include<stdlib.h>

struct passwd *mypwnam(const char *name){
    struct passwd *pwd;
    while((pwd = getpwent()) != NULL){
        if(strcmp(pwd->pw_name, name) == 0){
            setpwent();
            return pwd;
        }
    }
    setpwent();
    return NULL;
}

int main(int argc, char *argv[]){
    int i;
    struct passwd *p;
    if(argc < 2 || strcmp(argv[1], "--help") == 0)
    {
        printf("%s user-names...", argv[0]);
        exit(0);
    }

    for(i = 1; i < argc; ++i){
        p = mypwnam((const char *)argv[i]);
        if(p == NULL)
            printf("%s does not exit\n", argv[i]);
        else{
            printf("%s, UID: %ld, GID: %ld\n", argv[i], (long)p->pw_uid, (long)p->pw_gid);
        }
    }
    
    endpwent();
    exit(EXIT_SUCCESS);
}

